<?php

namespace App\Service\CallingCards;

use App\Library\Rms\RmsCurl;
use Illuminate\Support\Facades\Cache;

class ProductService
{
    public static function all(int $cacheMinute = 1): array
    {
        return Cache::remember(config('app.name') . '_' . static::class . '_' . __FUNCTION__, now()->addMinutes($cacheMinute), function () {
            $body = RmsCurl::getIns()->do('api/internal/productMenu/getAllById', [
                'id' => config('calling-cards.menuId'),
            ], 'GET');
            $list = $body['data']['list'] ?? [];
            return collect($list)->map(function ($item) {
                return [
                    'title' => $item['name'],
                    'sort'  => $item['sort'],
                    'items' => collect($item['list'])->map(function ($product) {
                        return [
                            'id'    => $product['id'],
                            'name'  => $product['name'],
                            'image' => $product['imageUrl'],
                        ];
                    })->toArray(),
                ];
            })->sortBy('sort')->toArray();
        });
    }
}
